//
// Created by 崔世强 on 2018/3/23.
// Copyright (c) 2018 Facebook. All rights reserved.
//

import Foundation
import WCDBSwift

enum GroupMemberType: Int {
    case normal = 9  //普通成员
    case owner = 1   //群主
    case admin = 2   //管理员
}

extension GroupMemberType: ColumnCodable,LiteralValueConvertible {
    init?(with value: FundamentalValue) {
        let v = Int(value.int32Value)
        self = GroupMemberType(rawValue: v) ?? GroupMemberType.normal
    }
    
    func archivedValue() -> FundamentalValue {
        return FundamentalValue(self.rawValue)
    }
    
    static var columnType: ColumnType {
        return .integer64
    }
}

final class GroupMember: TableCodable {
    static let table = "group_member"

    var id:Int = 0
    var userId: Int = 0
    var groupId: Int = 0
    var nickName: String?
    var mute: Bool = false
    var me: Bool = false
    var memberType: GroupMemberType = .normal
    var deleted: Bool = false

    enum CodingKeys: String, CodingTableKey {
        typealias Root = GroupMember
//        static let objectRelationalMapping = TableBinding(CodingKeys.self)
        case id
        case userId = "user_id"
        case groupId = "group_id"
        case nickName = "nickName"
        case mute
        case me
        case memberType = "member_type"
        case deleted
        
        
        static let objectRelationalMapping = TableBinding(CodingKeys.self) {
            BindColumnConstraint(.id, isPrimary: true, isAutoIncrement: true)
            BindIndex(userId.asIndex(), namedWith: "_userIdIndex")
            BindIndex(groupId.asIndex(), namedWith: "_groupIdIndex")
        }

//        static var columnConstraintBindings: [CodingKeys: ColumnConstraintBinding]? {
//            return [.id: ColumnConstraintBinding(isPrimary: true, isAutoIncrement: true)]
//        }
//
//        static var indexBindings: [IndexBinding.Subfix: IndexBinding]? {
//            return [
//                "_userIdIndex": IndexBinding(indexesBy: userId),
//                "_groupIdIndex": IndexBinding(indexesBy: groupId)
//            ]
//        }
    }

    var isAutoIncrement: Bool = true
    var lastInsertedRowID: Int = 0
}
